﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Circle.Domain.Interface.Service;
using Circle.Domain.Entity;
using Circle.Domain.Interface.Repository;
using Circle.Domain.Model;
using Circle.Domain.Interface;
using System.Data.Entity;
using Circle.Utility;

namespace Circle.Service
{
    public class CommentService : BaseOwnerCrudService<ContentComment>, ICommentService
    {
        private readonly IUploadFileService _uploadService;
        private readonly IUploadFileCategoryService _uploadCategoryService;
        private readonly IBannedWordService _bannedWordService;
        private readonly ISettingService _settingService;
        private readonly IRepository<ContentTopic> _topicRepository;

        private readonly Setting _defaultSetting;

        static CommentService()
        {
            PermissionNameDictionary.Add(EntityPermission.Read, PermissionConstants.Comment_Read);
            PermissionNameDictionary.Add(EntityPermission.Add, PermissionConstants.Comment_Add);
            PermissionNameDictionary.Add(EntityPermission.Update, PermissionConstants.Comment_Update);
            PermissionNameDictionary.Add(EntityPermission.Delete, PermissionConstants.Comment_Delete);
            PermissionNameDictionary.Add(EntityPermission.Vote, PermissionConstants.Comment_Vote);

            PermissionNameDictionary.Add(EntityPermission.UploadDocumentRead, PermissionConstants.Comment_Upload_Document_Read);
            PermissionNameDictionary.Add(EntityPermission.UploadDocument, PermissionConstants.Comment_Upload_Document);
            PermissionNameDictionary.Add(EntityPermission.UploadImage, PermissionConstants.Comment_Upload_Image);
        
        }

        public CommentService(IUploadFileService uploadService, IUploadFileCategoryService uploadCategoryService,
                        IBannedWordService bannedWordService, SettingService settingService, IRepository<ContentTopic> topicRepository)
        {
            _topicRepository = topicRepository;
            _uploadService = uploadService;
            _uploadCategoryService = uploadCategoryService;
            _bannedWordService = bannedWordService;
            _settingService = settingService;

            _defaultSetting = _settingService.GetDefaultSetting();
        }

        public override ContentComment Create()
        {
            var comment = base.Create();
            comment.With(x =>
            {
                x.Content = string.Empty;
                x.IsLocked = false;
                x.IsSolution = false;
                x.IsSpam = false;
                x.IsVisible = true;
                x.CreateTime = DateTime.UtcNow;
                x.LastUpdateTime = DateTime.UtcNow;
            });

            return comment;
        }

        protected override void OnSaving(ContentComment item, bool checkPermission)
        {
            item.LastUpdateTime = DateTime.UtcNow;
            item.OriginalContent = item.Content;
            item.Content = _bannedWordService.ReplaceBannedWord(item.OriginalContent, _defaultSetting.BannedWordReplaceHolder[0]);

            if (item.IsNew())
            {
                item.Topic = _topicRepository.All.Where(x => x.Id == item.TopicId).SingleOrDefault();
            }
        }

        protected override OperationStatus AddMark(ContentComment item)
        {
            item.CreateTime = DateTime.UtcNow;
            return base.AddMark(item);
        }

        protected override IQueryable<ContentComment> AddAlwaysPredication(IQueryable<ContentComment> source)
        {
            return source.Where(x => !x.IsAttachedToTopic);
        }

       
        public override bool HasPermission(EntityPermission permission, ContentComment item = null, Guid? threadId = null)
        {
            return true;
        }

    }
}
